package group.sicher.lock.factory;

import group.sicher.core.utils.SpringUtil;
import group.sicher.lock.component.MarsLockInterface;
import group.sicher.lock.component.impl.*;
import group.sicher.lock.constant.MarsLockType;

import java.util.EnumMap;

/**
 * @description:
 * @author: aron
 * @date: 2019-08-05 16:13
 */
public class MarsComponentFactory {

    private static EnumMap<MarsLockType, Class<?>> componentMap = new EnumMap<>(MarsLockType.class);

    static {
        componentMap.put(MarsLockType.FAIR, MarsFairLockInterfaceComponent.class);
        componentMap.put(MarsLockType.MULTI, MarsMultiLockInterfaceComponent.class);
        componentMap.put(MarsLockType.READ, MarsReadLockInterfaceComponent.class);
        componentMap.put(MarsLockType.WRITE, MarsWriteLockInterfaceComponent.class);
        componentMap.put(MarsLockType.REENTRANT, MarsReentrantLockInterfaceComponent.class);
        componentMap.put(MarsLockType.RED, MarsRedLockInterfaceComponent.class);
    }

    public MarsLockInterface getLock(MarsLockType type) throws Exception {
        MarsLockInterface marsLock = (MarsLockInterface) SpringUtil.getBean(componentMap.get(type));
        if (null == marsLock) {
            return null;
        }
        return marsLock;
    }


}
